package edu.ar.modulo3.perist.dao.impl.hbm;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;

import edu.ar.modulo3.model.Cliente;
import edu.ar.modulo3.perist.common.HibernateManager;
import edu.ar.modulo3.perist.dao.IClienteDAO;

public class ClienteDAOHbmImpl implements IClienteDAO{

	public void save(Cliente enttity) {
		// TODO Auto-generated method stub
		
	}

	public void update(Cliente enttity) {
		// TODO Auto-generated method stub
		
	}

	public void delet(Cliente enttity) {
		// TODO Auto-generated method stub
		
	}

	public List<Cliente> getAll() {
		
		List<Cliente> clienteResultList = null;
		Session session = HibernateManager.getSessionFactoryInstance().openSession();
		
		try {
		
			session.beginTransaction();
			
			String hqlQuery = "from Cliente";
			
			Query query = session.createQuery(hqlQuery);
						
			clienteResultList = query.list(); 
									
			session.getTransaction().commit();			
			
		} catch (Exception e) {			
			
			session.getTransaction().rollback();
			e.printStackTrace();
			return null;
			
		} finally {
			
			session.close();
			
		}
		
		return clienteResultList;
	}

	public Cliente getElemntById(Long id) {
		
		Cliente clienteResult = null;
		Session session = HibernateManager.getSessionFactoryInstance().openSession();
		
		try {
		
			session.beginTransaction();
		
			String hqlQuery = "from Cliente where idCliente = :idCliente";
			
			Query query = session.createQuery(hqlQuery);
			query.setParameter("idCliente", id);			
			
			clienteResult = (Cliente) query.uniqueResult(); 
									
			session.getTransaction().commit();			
			
		} catch (Exception e) {			
			
			session.getTransaction().rollback();
			e.printStackTrace();
			return null;
			
		} finally {
			
			session.close();
			
		}
		
		return clienteResult;
	}

}
